package com.xqledu.admin.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;


import java.util.Date;

/**
 * @author 向其林
 * @version 1.0
 * @description: TODO
 * @date 2023/7/4 0004 23:07
 */
public class JwtUtils {

    private static final String SECRET_KEY = "ZX7GwJZY+eb7+9eLwowQqeG6JiJLeMFmbnOUwkq28DU="; // 密钥，用于签名和验证 JWT
    private static final long EXPIRATION_TIME = 86400000; // 过期时间，单位毫秒（这里设置为24小时）

    // 生成JWT
    public static String generateJwt(String subject) {
        Date now = new Date();
        Date expiration = new Date(now.getTime() + EXPIRATION_TIME);

        return Jwts.builder()
                .setSubject(subject)
                .setIssuedAt(now)
                .setExpiration(expiration)
                .signWith(SignatureAlgorithm.HS256, SECRET_KEY)
                .compact();
    }

    // 解析JWT
    public static String parseJwt(String jwt) {
        Claims claims = Jwts.parser()
                .setSigningKey(SECRET_KEY)
                .parseClaimsJws(jwt)
                .getBody();

        return claims.getSubject();
    }
}
